package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResBanner;
import com.ruoyi.resturant.service.IResBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Anonymous
@RestController
@RequestMapping("/api/resturant/banner")
@Api(tags="轮播图管理")
public class ResBannerController {
    @Autowired
    private IResBannerService resBannerService;
    @Autowired
    private RedisCache redisCache;
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "轮播图名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pic", value = "轮播图图片地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "target_url", value = "轮播图图片链接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sort", value = "排序", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "轮播图状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "String", dataTypeClass = Date.class)
    })
    /**
     * 查询轮播图列表
     */
    @ApiOperation("查询轮播图列表")
    @ApiImplicitParam(name = "name", value = "轮播图名称",
            dataType = "string", dataTypeClass = String.class)
    /**
     获取所有商户列表
     * */

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String dishName) {
        List<ResBanner> list = resBannerService.selectResBannerByParam(dishName);
        return AjaxResult.success(list);
    }
    /**
     * 获取轮播图详细信息
     */

    @ApiOperation(value = "获取轮播图详细信息", notes = "TODO：取得某个轮播图的详细信息")
    @ApiImplicitParam(name = "id", value = "轮播图ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getRedisObj(@PathVariable("id") Long id){
            // 3 redis不存在这个轮播图的场合，从数据库里查询
            ResBanner resBanner = resBannerService.selectResBannerById(id);
            // 4 数据库里该轮播图ID不存在的场合，抛出业务异常BizException
            if(resBanner == null) {
                throw new BizException(404, "该轮播图id("+ id + ")不存在");
            }
            String strDB = JSON.toJSONString(resBanner);
            redisCache.setCacheObject(String.valueOf(id), strDB,
                    1, TimeUnit.HOURS);


        // 5 返回取得model对象
        return AjaxResult.success(resBanner);
    }


    /**
     * 新增轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.INSERT)

    @ApiOperation("新增轮播图")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResBanner resBanner) {
        return AjaxResult.success(resBannerService.insertResBanner(resBanner));
    }


    /**
     * 修改轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.UPDATE)

    @ApiOperation("修改轮播图")
    @PutMapping
    public AjaxResult edit( @RequestBody ResBanner resBanner) {
        //1先根据轮播图ID查询数据库
        ResBanner resBannerDB=  resBannerService.selectResBannerById((long) resBanner.getId());
        //如果该轮播图不存在，就抛出业务异常
        if (resBannerDB == null){
            throw  new BizException(404,"该轮播图id"+resBanner.getId()+"不存在");
        }
        //如果轮播图存在，执行更新操作
        return AjaxResult.success(resBannerService.updateResBanner(resBanner));
    }

    /**
     * 删除轮播图
     */
    @Log(title = "轮播图管理",businessType = BusinessType.DELETE)

    @ApiOperation("删除轮播图")
    @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resBannerService.deleteResBannerById(id));
    }
}
